What is the wrapper class for the primitive numeric type int
?
Integer
(It would not be too surprising if you forgot that.) Here is a program that computes the square of a number that the user enters. It uses the one-line method of constructing a BufferedReader.
import java.io.*; class EchoSquare { public static void main (String[] args) throws IOException { BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in)); String inData; int num, square; // declare two int variables System.out.println("Enter an integer:"); inData = stdin.readLine(); num = Integer.parseInt( inData ); // convert inData to int square = num * num ; // compute the square System.out.println("The square of " + inData + " is " + square); } }
This program is the previous program with a few additions (in blue). You can copy-paste-and-run it. Better yet, study it for a while, then go to NotePad and see how much you can reconstruct. Now look at this statement:
num = Integer.parseInt( inData ); // convert inData to int
This uses the method parseInt()
of the Integer
wrapper class.
This method takes a String containing an integer in
character form.
It looks at those characters and calculates
an int
value.
That value is assigned to num
.